home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / VBASIC / DLL_OTHR.ZIP / DLLTUT.TXT < prev    next >
Encoding:
Text File  |  1995-11-17  |  9.0 KB  |  136 lines

  1. A Short Tutorial on Writing a DLL
  2. Glenn D. Jones
  3. November 8,1995
  4. Introduction
  5. This tutorial will explain the basics of writing a Windows DLL.  I will direct it at Visual Basic programmers who have decided they need to write a DLL for their VB applications.  However, VB programmers not are the only users of DLLs.  Those programmers who want to write a DLL to interface with other languages will also find this tutorial useful.
  6.  
  7. In addition this tutorial is specific to writing a DLL using C.
  8. What is a Dynamic Link Library?
  9. A Dynamic Link Library, DLL, is a library of functions callable by an application at runtime. The application and functions within the DLL are not bound until the application program is executed.
  10.  
  11. Normally the functions in a DLL are for a particular purpose (internet access, graphing, serial port communication).  They may also be a collection of functions required by an application and only be specific to that application.  This is only convention, if you are writing a DLL you can put whatever functions you want into it.
  12.  
  13. If you are going to write and distribute a DLL, you do not need to register its name nor get an ID from anyone.  You can name it whatever you want, but please try to make it unique.  If you call your DLL "KERNEL.DLL" you will have problems with it (KERNEL is a Microsoft Windows DLL used to perform operating system kernel functions).
  14.  
  15. An application can bind to a DLL in two ways:
  16.  
  17. Load Time Binding
  18. On Call Binding
  19.  
  20. Load Time Binding loads the DLL when an application that uses it is loaded for execution.  Windows loads the DLL for the application.
  21.  
  22. With On Call Binding, the DLL is loaded when it is needed.  If it is never needed by the application it will never be loaded. 
  23.  
  24.  
  25. Visual Basic loads DLLs when the form that contains their Declare statement is loaded.  If a VB application places the DLL function declare in a module or on a form that is loaded when the application is loaded, then the DLL will be bound at application load time.  If the declare is placed in a form that is dynamically loaded by the VB application, then the DLL will be loaded when the form is loaded.
  26.  
  27. One last note on loading DLL's, Windows will locate a DLL in the following order:
  28.  
  29. In memory, a DLL, once loaded, is global to all of Windows until it is unloaded.
  30. In the directory where the application was loaded from
  31. In the windows directory
  32. In the windows\system directory
  33. In directories specified in the DOS PATH
  34.  
  35.  
  36. What do I need to write a DLL?
  37. As of today, most DLL's are written in C.  It is possible to write a DLL in PASCAL/Delphi or assembly language. (There was a product on the market that allowed DLL's to be written in Basic but I have not seen mention of it for while.  If anyone knows of this, please let me know.)
  38.  
  39. In order to write a DLL you will need a compiler capable of creating Windows DLL.  The four most popular C compilers that can do this are:
  40.  
  41. Microsoft Visual C++
  42. Borland C++ for Windows
  43. Symantec C++ for Windows
  44. Watcom C++
  45.  
  46. If you do not have a compiler, you will need one to write a DLLs.
  47.  
  48. In addition to the compiler, you will need knowledge of C and the Windows API.
  49.  
  50. NOTE: C is the language that is used to write a DLL, not C++.  C++ is an object oriented extension of C.  If you have a C++ compiler, it can handle C just fine.  You can also write non-exported functions as C++ objects, but the functions that are called into from Windows applications must be C functions, not C++ functions or methods.
  51.  
  52. DLL Requirements
  53. A Windows DLL requires only one function called LibMain.  If a DLL does not have a LibMain it will not work.  
  54.  
  55. Windows calls the LibMain function of a DLL to initialize the DLL.  For most simple DLL's, you code LibMain as follows:
  56.  
  57. int FAR PASCAL _export LibMain(HANDLE hInstance,WORD wDataSeg,
  58. WORD wHeapSize,LPSTR lpszCmdLine)
  59. {
  60. if(wHeapSize > 0)
  61. UnlockData(0);
  62.  
  63. return(1);
  64. }
  65. You can find details of LibMain in the Windows API documentation.
  66.  
  67. The only other requirement  of functions in a DLL is that those functions that can be called from outside the DLL, must be declared "FAR PASCAL _export".  If you do not declare them this way, you will not be able to call them.
  68.  
  69. You will also need a special DEF file for a DLL.  Below is a sample that was generated by Microsoft Visual C++.  Most of today's compilers will generate the DEF for you, but if yours does not, create one that looks like this:
  70.  
  71. LIBRARY   <your DLL name goes here>
  72. EXETYPE   WINDOWS
  73. CODE      PRELOAD MOVEABLE DISCARDABLE
  74. DATA      PRELOAD MOVEABLE SINGLE
  75. HEAPSIZE  1024
  76. EXPORTS
  77.           WEP PRIVATE
  78. ; To implement your own Windows Exit Procedure add the following
  79. ; function to your application (referring to it in the .def file is
  80. ; not required.)  The extern "C" is only required if module is C++.
  81. ; extern "C" int FAR PASCAL _WEP(int)
  82. ; {
  83. ;       /* Your WEP functionality goes here */
  84. ;  return 1;
  85. ; }
  86.  
  87. That is it.  After you compile and link, you can begin testing your DLL.
  88.  
  89. The samples that go with this tutorial contain all of this basic information.  You can use it as a template to start writing your first DLL.
  90. What can I do in a DLL?
  91. Many people ask what is permissible in a DLL.  Well, just about anything is permissible.  When an application calls a DLL function, the DLL is executing as an extension of the application's task.  Any files it opens are open to the task; any memory allocated is allocated from the task's local or global heaps.  Just keep this in mind and you will be fine.
  92.  
  93. Let's look at a small example.  If you write a DLL that executes a LocalAlloc API call for 32000 bytes and it does not call a LocalFree on that memory handle before returning, the DLL will have just taken 32000 bytes of the applications local heap.  You will probably start getting out of memory messages from the application.
  94.  
  95. Some of the things you might want to do in a DLL are:
  96.  
  97. Make GDI function calls to paint the screen.
  98. Allocate Windows global memory
  99. Perform file I/O
  100. Search Windows internal tables
  101. Perform port I/O
  102. Call other API's
  103.  
  104. All of these things are possible, the most important thing to remember is to write your DLL as clean as possible.  If you allocate a resource, and you do not need to keep it, free it before you return from your DLL function.  Always remember, a DLL can cause all kinds of problems in Windows and they might not show until your DLL has long been unloaded.
  105.  
  106. Also remember that a DLL is global.  If you store data in a global variable inside a DLL and two tasks are currently using the DLL, they will both be accessing the same global area in the DLL.  In addition, when Windows unloads the DLL, you lose the global data.
  107.  
  108. BE CAREFUL AND KEEP IT CLEAN.
  109.  
  110.  
  111. Where can I get sample DLL source?
  112. You can find source code for DLL's on almost any Internet FTP site dealing with Windows programming as well as other on-line service forums.  The problem with most of this source is that the DLL was written for a purpose.  You have to wade through hundreds of lines of code just to get an idea of what you need for a simple DLL.  
  113.  
  114. Included with this tutorial is a sample DLL that does nothing but add two arguments and return the result.  It contains the source code and project files for both Borland C++ and Microsoft Visual C++.  In addition it contains a VB program, with source, that calls  the DLL.
  115.  
  116. If there is a demand for more complicated DLL's (maybe some showing how to perform GDI calls or access VBX control information) I could probably be persuaded to add more samples to this tutorial.  Let me know.
  117.  
  118. Where can I get more information on writing a DLL?
  119. Most good books on C programming for Windows will include a chapter or two on writing a DLL.  Browse your local book store and find one you like.
  120.  
  121. Since writing a DLL is really just writing a Windows program, you will need access to the Windows API documentation.  You will also want to get information on writing programs using GDI, KERNEL and other part of Windows.
  122.  
  123. About the author
  124. I, Glenn Jones, have been a professional programmer for 16 years.  For most of this time I have been designing, writing and supporting system software (compilers, debuggers, code generators, communication API's ect).  I have worked in a variety of environments and with many different languages and compilers.  At this point I am dedicated to describing and helping programmers with the debugging process.  I believe that the first step in debugging a program is understanding the principles of the environment for which you are writing.  If you need to write and debug a DLL, then you need to know how to write a DLL correctly.
  125.  
  126. If you have any questions or comments about this tutorial or suggestions for future tutorials, please contact me at gdjones@ix.netcom.com.
  127.  
  128. Have fun.
  129.  
  130. Copyright and Distribution
  131. This document is Copyright ( 1995 by Glenn D. Jones.  It may be distributed freely for non-commercial use.  It may not be included in print or electronically published for sale without the permission of the author.
  132.  
  133. Microsoft Visual C++, Microsoft Visual Basican Windows are copyright Microsoft Corporation.
  134. Borland C++ is copyright Borland International, Inc.
  135.  
  136.